<?php

namespace app\config\actions;

use \Yii;

class ConfigAction extends \yii\base\Action {

    public $modelClass;

    public $template;

    public function run() {
        $model = \Yii::createObject([
            "class" => $this->modelClass
        ]);
        $model->loadDefaultValues();
        $this->controller->performAjaxValidation($model);

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->controller->flash('success', "更新成功");
            } else {
                $this->controller->flash('error', '更新失败，' . $model->formatErrors());
            }
            return $this->controller->refresh();
        }

        return $this->controller->render($this->getTemplate(), [
            "model" => $model
        ]);
    }

    public function getTemplate() {
        if ($this->template != null) {
            return $this->template;
        }

        return $this->id;
    }
}